package org.nfIt.controller;

import org.apache.ibatis.annotations.Delete;
import org.nfIt.entity.Category;
import org.nfIt.entity.Result;
import org.nfIt.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClossName:CategoryController
 * Pakage:org.nfIt.controller
 * Description
 *
 * 文章
 *
 * @Author L
 * @Create 2024/6/5 16:04
 * @Version 2023.1.3
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加文章分类
     * #@RequestBody Category category: 交给框架处理, @RequestBody注解 会从请求体得json字符串封装为 Category 实体
     * @param category 实体
     */
    @PostMapping
    public Result addCategory(@RequestBody @Validated(Category.Add.class) Category category) {
        categoryService.add(category);
        return Result.success();
    }

    /**
     * 获取所有的文章信息
     * @return
     */
    @GetMapping
    public Result<List<Category>> list() {
        List<Category> list = categoryService.list();
        return Result.success(list);
    }

    /**
     * 根据id获取文章信息
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result<Category> detail(Integer id) {
       Category category = categoryService.findByid(id);
        return Result.success(category);
    }

    /**
     * 更新分类信息
     * # @Validated(Category.update.class) 使用分组校验数据
     * @param category 实体
     */
    @PutMapping
    public Result<Category> update(@RequestBody @Validated(Category.update.class) Category category) {
        categoryService.update(category);
        return Result.success();
    }

    /**
     * 根据id删除
     * @param id id
     * @return
     */
    @DeleteMapping
    public Result delete(Integer id){
        categoryService.delete(id);
        return Result.success();
    }


}
